package com.cskaoyan.order.service.impl;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.exception.BaseBusinessException;
import com.cskaoyan.order.constant.OrderRetCode;
import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderQueryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhuqiushi
 * @since 2022/09/21 14:43
 */
@Service
public class OrderQueryServiceImpl implements OrderQueryService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    OrderShippingMapper orderShippingMapper;
    @Autowired
    OrderConverter orderConverter;

    @Override
    public OrderListResponse orderList(OrderListRequest request) {
        OrderListResponse result = new OrderListResponse();
        //做一下分页
        PageHelper.startPage(request.getPage(), request.getSize());
        //在这里查order表
        Example oExample = new Example(Order.class);
        if (!StringUtils.isEmpty(request.getSort())) {
            oExample.orderBy(request.getSort() + " desc");
        }
        oExample.createCriteria().andEqualTo("userId", request.getUserId());
        List<Order> orderPOs = orderMapper.selectByExample(oExample);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orderPOs);
        long total = orderPageInfo.getTotal();

        //使用convert
        List<OrderDetailInfo> orders =
                orderPOs.stream() .sorted((a,b) -> {
                            if (a.getUpdateTime().before(b.getUpdateTime())) {
                                return 1;
                            }
                            return -1;
                        }
                ).map(u -> orderConverter.order2detail(u)).collect(Collectors.toList());

        for (OrderDetailInfo order : orders) {
            //再查order-item表
            Example oIExample = new Example(OrderItem.class);
            oIExample.createCriteria().andEqualTo("orderId", order.getOrderId());
            List<OrderItem> orderItemPOs = orderItemMapper.selectByExample(oIExample);
            //类型转换
            List<OrderItemDto> orderItems =
                    orderItemPOs.stream().map(u -> orderConverter.item2dto(u)).collect(Collectors.toList());
            //为order的属性赋值
            order.setOrderItemDto(orderItems);
            //再查order-shipping表
            Example oSExample = new Example(OrderShipping.class);
            oSExample.createCriteria().andEqualTo("orderId", order.getOrderId());
            List<OrderShipping> orderShippingPOs = orderShippingMapper.selectByExample(oSExample);
            //类型转换
            List<OrderShippingDto> orderShippings =
                    orderShippingPOs.stream().map(u -> orderConverter.shipping2dto(u)).collect(Collectors.toList());
            //为order属性赋值
            if (orderShippings.size() != 1) {
                throw new BaseBusinessException(SysRetCodeConstants.DB_EXCEPTION.getCode(),
                        SysRetCodeConstants.DB_EXCEPTION.getMessage());
            }
            order.setOrderShippingDto(orderShippings.get(0));
        }
        //清除分页器缓存
        PageHelper.clearPage();
        result.setData(orders);
        result.setTotal(total);
        return result;
    }

    @Override
    public OrderDetailResponse orderDetail(String orderId) {
        OrderDetailResponse result = new OrderDetailResponse();
        //在这里先查order
        Order order = orderMapper.selectByPrimaryKey(orderId);
        //再查order-item
        OrderItem orderItem = new OrderItem();
        orderItem.setOrderId(orderId);
        List<OrderItem> orderItems = orderItemMapper.select(orderItem);
        //类型转换
        List<OrderItemDto> orderItemDtos =
                orderItems.stream().map(u -> orderConverter.item2dto(u)).collect(Collectors.toList());
        //赋值
        result.setGoodsList(orderItemDtos);
        //再查order-shipping
        OrderShipping orderShipping = new OrderShipping();
        orderShipping.setOrderId(orderId);
        List<OrderShipping> orderShippings = orderShippingMapper.select(orderShipping);
        if (orderShippings.size() != 1 || orderItemDtos.size() < 1) {
            throw new BaseBusinessException(SysRetCodeConstants.DB_EXCEPTION.getCode(),
                    SysRetCodeConstants.DB_EXCEPTION.getMessage());
        }
        //类型转化
        result = orderConverter.order2res(order, orderShippings.get(0));
        result.setGoodsList(orderItemDtos);
        return result;
    }
}
